package com.eascs.admin.recordlog.servcie;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.admin.common.service.BaseSevice;
import com.eascs.admin.loanapplyoplog.O2oRecordLog;
import com.eascs.admin.o2o.loan.dao.LoanapplyOpLogDao;
import com.eascs.admin.recordlog.constant.O2oLogType;
import com.eascs.admin.recordlog.dao.O2oRecordLogDao;
import com.eascs.admin.recordlog.vo.O2oRecordLogVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;

/**
 * @ClassName: O2oRecordLogService.java
 * @Description:
 * 				<p>
 *               日志Service
 *               </p>
 * @author CXW
 * @date 2016年9月22日 下午4:41:30
 * @version V1.0
 */

@Service
public class O2oRecordLogService extends BaseSevice{

	@Autowired
	O2oRecordLogDao logDao;
	
	@Autowired
	LoanapplyOpLogDao loanapplyOpLogDao;

	public PageResult<Object[]> findByPage(O2oRecordLogVo vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append("SELECT lol.log_id, lol.op_psn, lol.op_date, lol.op_type, lol.op_type_name ");
		querySql.append("FROM loanapply_op_log AS lol where 1=1 ");
		if(!StringUtil.isEmpty(vo.getOperUserName())){
			querySql.append(" and lol.op_psn like ?");
			sqlPageQuery.addLikeParam(vo.getOperUserName());
		}
		if(!StringUtil.isEmpty(vo.getLogType())){
			querySql.append(" and lol.op_type = ?");
			sqlPageQuery.addParam(vo.getLogType());
		}
		querySql.append(" order by lol.op_date desc");
		sqlPageQuery.setQuerySql(querySql.toString());
		return logDao.findDataWithPage(sqlPageQuery);
	}
	
	public PageResult<Object[]> findPartnerLogByPage(O2oRecordLogVo vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append("SELECT lol.log_id, lol.op_psn, lol.op_date, lol.op_type, lol.op_type_name ");
		querySql.append("FROM loanapply_op_log AS lol where 1=1 and lol.op_type in ('PT','RD','APT','ARD') ");
		if(!StringUtil.isEmpty(vo.getLogType())){
			querySql.append(" and lol.op_content_up = ?");
			sqlPageQuery.addParam(vo.getLogType());
		}
		querySql.append(" order by lol.op_date desc");
		sqlPageQuery.setQuerySql(querySql.toString());
		return logDao.findDataWithPage(sqlPageQuery);
	}
	
	
	@Transactional(transactionManager = "o2oTransactionManager")
	public int saveEntityLog(O2oRecordLog recordLogEntity) {
		return loanapplyOpLogDao.insert(recordLogEntity);
	}

	/**
	 * 保存结清、作废操作日志
	 * @param billCode
	 * @param opPsn
	 * @param type
	 * @param settleDate
	 * @param remark
	 */
	@Transactional(transactionManager = "o2oTransactionManager")
	public void saveLog(String billCode, String opPsn, String type, Date settleDate, String remark, String motion) {
		// 保存结清、作废操作日志
		O2oRecordLog log = new O2oRecordLog();
		try {
			log.setBillCode(billCode);
			log.setOpPsn(opPsn);
			log.setOpDate(new Date());
			log.setOpType(type);
			log.setOpTypeName(O2oLogType.getStringName(type));
			log.setSettleDate(settleDate);
			log.setRemark(remark);
			log.setMotion(motion);

			saveEntityLog(log);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
